﻿using Dapper;
using HomagChina.Basic.CCT.Db.CCT_Db;
using Serilog.Core;
using System;
using System.Data.SqlClient;
using System.Linq;
using Telerik.Windows.Data;

namespace Proxy
{
    public class InsertInfoHandle
    {
        public InsertInfoHandle()
        {

        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns></returns>
        public bool InsertBoradInfo(T_BoardInfo t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var info = Db.T_BoardInfo.Where(s => s.Reference.Equals(t.Reference)).ToList();
                    if (info.Count > 0)
                    {
                        Db.T_BoardInfo.RemoveRange(info);
                    }
                    Db.T_BoardInfo.Add(t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        public bool InsertSettingsHandle(T_MachineInfo t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_MachineInfo.Add(t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        public bool InsertDirectionHandle(T_DirectionConfig t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_DirectionConfig.Add(t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertOnlinePart(T_TransOnlineRecords T)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_TransOnlineRecords.Add(T);
                    result = Db.SaveChanges() > 0;
                }
                InsertScanRecords(T.Reference);
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertScanRecords(string Reference)
        {
            T_ScanRecords_Boards T = new T_ScanRecords_Boards();
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T.Upi = Reference;
                    Db.T_ScanRecords_Boards.Add(T);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertMprRecord(string Reference)
        {
            bool result = false;

            try
            {
                T_SendRecords_Mpr t_SendRecords_Mpr = new T_SendRecords_Mpr()
                {
                    Reference = Reference,
                    MprFlag = 1,
                    SendTime = DateTime.Now,
                };
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_SendRecords_Mpr.Add(t_SendRecords_Mpr);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }


        /// <summary>
        /// 给CNC添加加工任务
        /// 
        /// create a job to cnc machine
        /// </summary>
        /// <param name="Ip"></param>
        /// <param name="Reference"></param>
        /// <param name="MprName"></param>
        /// <param name="Mode"></param>
        /// <returns></returns>
        public bool InsertJob2CNC(string Ip, string Reference, int Orientation, string MprName, int Mode, Logger log)
        {
            bool result = false;
            try
            {
                string ConnectString = "Data Source=" + Ip + ";Initial Catalog=INTERFACE_BHX310;User ID=hgservice;Password=Homag";
                string sql = "Insert Into tbHomagCNCInPut(State,UpdateDate,CreateDate,Reference,Orientation,MprFile,Modus) " +
                                                    "values(10,getdate(),getdate(),'" + Reference + "'," + Orientation + ",'" + MprName + "','" + Mode + "')";
                using (var connection = new SqlConnection(ConnectString))
                {
                    try
                    {
                        connection.Open();
                        if (result = connection.Execute(sql) > 0)
                        {
                            InsertMprRecord(Reference);
                        }
                        connection.Close();
                    }
                    catch (Exception ex)
                    {
                        connection.Close();
                    }
                }
                log.Information("Reference :" + Reference + "-> 发送MPR");
                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 添加工程单信息
        /// 
        /// 
        /// </summary>
        /// <param name="Reference"></param>
        /// <returns></returns>
        public bool InsertProjectOrder(string Reference, float Length, float Width, float Thickness)
        {
            bool result = false;
            try
            {
                string ConnectString = "Data Source=MT259730180M11;Initial Catalog=CellControl;User ID=hgservice;Password=Homag";
                //string ConnectString = "Data Source=shh-svdb02\\mssqlserver01;Initial Catalog=SHH-CELLCONTROLDB01;User ID=cellcontrolsa;Password=Dd!!377470377470";

                string sql_Delete = "Delete T_ProjectOrder where Reference = '" + Reference + "'";

                string sql_Update = "Update T_ProjectOrder set State = 0";

                string sql_Insert = "Insert Into T_ProjectOrder(Reference,Length,Width,Thickness,State,MprName,CreateTime,UpdateTime) " +
                                                    "values('" + Reference + "'," + Length + "," + Width + "," + Thickness + ",1,'P:\\" + Reference + ".mpr',getdate(),getdate())";
                using (var connection = new SqlConnection(ConnectString))
                {
                    try
                    {
                        connection.Open();
                        connection.Execute(sql_Delete);
                        connection.Execute(sql_Update);
                        result = connection.Execute(sql_Insert) > 0;
                        connection.Close();
                    }
                    catch (Exception ex)
                    {
                        connection.Close();
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}